<template>
	<view class="myTracks">
		<!-- 返回首页 -->
		<uni-bankhome />
		<u-navbar title="我的足迹">
			<!-- <div @click="changeRightBtn" slot="right" style="margin-right:32rpx">
        {{edit}}
      </div> -->
		</u-navbar>
		<view class="u-tabs-box">
			<u-tabs :active-color="lightColor" class="slot-wrap" :list="navList" count="count" :is-scroll="true"
				:current="tabCurrentIndex" @change="tabClick"></u-tabs>
		</view>
		<view v-if="tabCurrentIndex == 0" class="tab-content">
			<scroll-view class="list-scroll-content" scroll-y>
				<!-- 空白页 -->
				<u-empty style="padding-top: 40rpx" text="暂无历史商品记录" mode="favor" v-if="goodsEmpty"></u-empty>
				<!-- 商品展示数据 -->
				<view v-for="(value, key) in goodsTrack " :key="key" v-else>
					<div style="padding: 20upx;">{{key}}</div>
					<view>
						<!-- <view class="myTracks-title" @click="navgaiteToStore(item)">{{item.storeName}}</view> -->
						<view class="myTracks-items">
							<view class="myTracks-item" v-for="(item, index) in value" :key="index">
								<!-- <u-checkbox-group>
									<u-checkbox v-model="item.___isDel" v-if="editFlag" active-color="#ff6b35"
										style="margin-right: 10rpx" @change="changeChecked(item)"></u-checkbox>
								</u-checkbox-group> -->
								<view>
									<view class="myTracks-item-img" @click="goGoodsDetail(item)">
										<image :src="item.thumbnail"></image>
									</view>
									<view class="myTracks-item-content" @click="goGoodsDetail(item)">
										<!-- <view class="myTracks-item-title">
											{{ item.goodsName }}
											<view class="myTracks-item-title-desc"> </view>
										</view> -->
										<view class="myTracks-item-price">
											{{ item.price | unitPrice }}
										</view>
									</view>
								</view>
							</view>
						</view>
						<view class="myTracks-divider"></view>
					</view>
				</view>
				<uni-load-more :status="loadStatus"></uni-load-more>
			</scroll-view>
		</view>
		<!-- 显示足迹的店铺栏 -->
		<view v-else class="tab-content">
			<scroll-view class="list-scroll-content" scroll-y>
				<!-- 空白页 -->
				<u-empty style="margin-top: 40rpx" text="暂无历史店铺记录" mode="favor" v-if="storeEmpty"></u-empty>
				<!-- 店铺展示数据 -->
				<view v-for="(value, key) in  storesTrack" :key="key" v-else>
					<div style="padding: 20upx;">{{key}}</div>
					<u-swipe-action btn-width="180" v-for="(item, index) in value" :key="index">
						<view class="store" @click="goStoreMainPage(item.id)">
							<view class="intro">
								<view class="store-logo">
									<u-image width="102rpx" height="102rpx" :src="item.storeLogo" :alt="item.storeName"
										mode="aspectFill">
										<u-loading slot="loading"></u-loading>
									</u-image>
								</view>
								<view class="store-name">
									<view>{{ item.storeName }}</view>
									<!-- <u-tag size="mini" type="error" :color="$mainColor" v-if="item.selfOperated"
										text="自营" mode="plain" shape="circle" /> -->
								</view>
								<view class="store-collect">
									<view>进店逛逛</view>
								</view>
							</view>
						</view>
					</u-swipe-action>
				</view>
				<uni-load-more :status="loadStatus"></uni-load-more>
			</scroll-view>
		</view>
		<view v-if="editFlag">
			<view class="myTracks-action">
				<view class="myTracks-action-check">
					<u-checkbox-group>
						<u-checkbox v-model="allChecked" v-if="editFlag" active-color="#ff6b35"
							style="margin-right: 10rpx" @change="checkedAllitem"></u-checkbox>
						全选
					</u-checkbox-group>
				</view>

				<!-- <view>
					<u-button type="warning" @click="delAllTracks" class="myTracks-action-btn">
						删除
					</u-button>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		myTrackGoodsList,
		deleteHistoryListId,
	} from "@/api/members.js";

	export default {
		data() {
			return {
				lightColor: this.$lightColor,
				tabCurrentIndex: 0, //tab的下标默认为0，也就是说会默认请求商品
				edit: "编辑",
				editFlag: false, //是否编辑
				allChecked: false, //是否全选
				loadStatus: "more", //底部下拉加载状态
				navList: [
					//tab显示数据
					{
						name: "商品(0)",

						params: {
							pageNumber: 1,
							pageSize: 10,
						},
					},
					{
						name: "店铺(0)",

						params: {
							pageNumber: 1,
							pageSize: 10,
						},
					},
				],
				goodsTrack: {}, //按时间浏览足迹商品
				storesTrack: {}, //按时间浏览足迹商品
				goodsEmpty: false, //商品数据是否为空
				storeEmpty: false, //店铺数据是否为空
				goodList: [], //商品集合
				storeList: [], //店铺集合
			};
		},

		/**
		 * 滑到底部加载下一页数据
		 */
		onReachBottom() {
			if (this.loadStatus != "noMore") {
				if (this.tabCurrentIndex == 0) {
					this.navList[0].params.pageNumber++;
					this.getGoodList();
				} else {
					this.navList[1].params.pageNumber++;
					this.getStoreList();
				}
			}
		},
		onLoad() {
			this.getGoodList();
			this.getStoreList();
		},
		onPullDownRefresh() {
			if (this.tabCurrentIndex == 0) {
				this.goodList = [],
					this.getGoodList();
			} else {
				this.storeList = [],
					this.getStoreList();
			}
		},
		methods: {
			/**
			 * 顶部tab点击
			 */
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			/**
			 * 导航到店铺
			 */
			navgaiteToStore(val) {
				uni.navigateTo({
					url: "/pages/product/shopPage?id=" + val.storeId,
				});
			},

			/**
			 * 设置右侧导航栏文本
			 */
			setStyle(text) {
				//导航按钮文本设置
				let pages = getCurrentPages();
				let page = pages[pages.length - 1];
				// #ifdef APP-PLUS
				let currentWebview = page.$getAppWebview();
				let titleNView = currentWebview.getStyle().titleNView;
				titleNView.buttons[0].text = text;
				if (text == "完成") {
					this.trackList.forEach((key) => {
						key.history.forEach((item) => {
							this.$set(item, "___isDel", false);
						});
					});
				}
				currentWebview.setStyle({
					titleNView: titleNView,
				});
				// #endif
				// #ifdef H5
				// h5 临时方案
				document.getElementsByClassName("uni-btn-icon")[1].innerText = text;
				// #endif
			},

			/**
			 * 跳转商品详情
			 */
			goGoodsDetail(item) {
				uni.navigateTo({
					url: "/pages/product/goods?id=" + item.id + "&goodsId=" + item.goodsId,
				});
			},
			/**
			 * 查看店铺详情
			 */
			goStoreMainPage(id) {
				//店铺主页
				uni.navigateTo({
					url: "/pages/product/shopPage?id=" + id,
				});
			},
			/**
			 * 获取商品集合
			 */
			getGoodList() {
				uni.showLoading({
					title: "加载中",
				});
				myTrackGoodsList(this.navList[0].params, "footprint").then((res) => {
					uni.stopPullDownRefresh();
					uni.hideLoading();
					if (res.data.success) {
						let goodsTrack = {}
						let data = res.data.result;
						this.navList[0].name = `商品(${data.length})`;
						res.data.result &&
							res.data.result.forEach((item) => {
								item.___isDel = false;
							});
						data.map(item => {
							if (item.footTime in goodsTrack) {
								goodsTrack[item.footTime].push(item)
							} else {
								goodsTrack[item.footTime] = [item]
							}
							return goodsTrack
						})
						this.goodsTrack = goodsTrack
						// console.log('@@@@', this.goodsTrack)
						if (data.length == 0) {
							this.goodsEmpty = true;
						} else if (data.length < 10) {
							this.loadStatus = "noMore";
							this.goodList.push(...data);
						} else {
							this.goodList.push(...data);
							if (data.length < 10) this.loadStatus = "noMore";
						}
					}
				});
			},

			/**
			 * 获取店铺集合
			 */
			getStoreList() {
				uni.showLoading({
					title: "加载中",
				});
				myTrackGoodsList(this.navList[1].params, "footStore").then((res) => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					if (res.data.success) {
						let storesTrack = {}
						let data = res.data.result;
						this.navList[1].name = `店铺(${data.length})`;
						data.map(item => {
							if (item.footTime in storesTrack) {
								storesTrack[item.footTime].push(item)
							} else {
								storesTrack[item.footTime] = [item]
							}
							return storesTrack
						})
						this.storesTrack = storesTrack
						// console.log('@@@@', this.storesTrack)
						if (data.length == 0) {
							this.storeEmpty = true;
						} else if (data.length < 10) {
							this.loadStatus = "noMore";
							this.storeList.push(...data);
						}
					}
				});
			},

			/**
			 * 点击后判断是不是全选
			 */

			changeChecked(val) {
				var opt = null;
				const isCheckedAll = this.trackList.every((key) => {
					key.___isDel == val.___isDel;
					opt = key.___isDel;
					return opt;
				});
				if (opt) {
					this.allChecked = isCheckedAll;
				} else {
					this.allChecked = false;
				}
			},

			/**
			 * 右侧标签栏切换
			 */
			changeRightBtn(e) {
				if (!this.editFlag) {
					this.edit = "完成";
				} else {
					this.edit = "编辑";
				}
				this.editFlag = !this.editFlag;
			},

			/**
			 * 点击全选按钮
			 */
			checkedAllitem() {
				//全选按钮
				this.trackList.forEach((key) => {
					this.$set(key, "___isDel", this.allChecked);
				});
			},
		},
	};
</script>

<style lang="scss">
	.myTracks {
		width: 100%;
		padding-top: 2rpx;
	}

	.u-tabs-box {
		position: relative;
		z-index: 10;
	}

	.myTracks-title {
		width: 100%;
		height: 110rpx;
		padding-left: 20rpx;
		font-size: 28rpx;
		color: #666;
		font-weight: bold;
		background-color: #fff;
		align-items: center;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
	}

	.myTracks-items {
		width: 100%;
		padding-top: 2rpx;
		align-items: center;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		// flex-direction: column;
		justify-content: flex-start;
		flex-wrap: wrap;
	}

	.myTracks-item {
		// width: 100%;
		height: 226rpx;
		margin: 20upx;
		// border-radius: 6/@px;
		background-color: #fff;
		position: relative;
		align-items: center;
		display: -webkit-box;
		display: -webkit-flex;
		width: 28%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.myTracks-item-content {
		text-align: center;
	}

	.myTracks-item-img {
		// margin-right: 20rpx;
		border-radius: 8rpx;

		image {
			width: 130rpx;
			height: 130rpx;
			border-radius: 8rpx;
		}
	}

	.myTracks-item-title {
		font-size: 28rpx;
		color: #333;
	}

	.myTracks-item-title-desc {
		font-size: 28rpx;
		color: #999;
	}

	.myTracks-item-price {
		font-size: 28rpx;
		color: $light-color;
		padding: 10rpx 0 0 0;
	}

	.myTracks-action {
		display: flex;
		justify-content: space-between;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		background: #fff;
		height: 75rpx;
		align-items: center;
		padding: 0 32rpx;
	}

	.myTracks-action-btn {
		width: 130rpx;
		height: 60rpx;
		line-height: 60rpx;
	}

	.myTracks-divider {
		width: 100%;
		height: 20rpx;
	}



	.myTracks-action-check {
		align-items: center;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
	}

	page,
	.content {
		background: $page-color-base;
		height: 100%;
	}

	.slot-wrap {
		flex: 1;
		display: flex;
		justify-content: center;
		width: 100%;
	}

	.content {
		width: 100%;
	}

	.list-scroll-content {
		height: 100%;
		width: 100%;
	}

	/deep/ .u-swipe-content {
		overflow: hidden;
	}

	.goods {
		background-color: #fff;
		border-bottom: 1px solid $border-color-light;
		height: 190rpx;
		display: flex;
		align-items: center;
		padding: 30rpx 20rpx;
		margin-top: 20rpx;

		image {
			width: 131rpx;
			height: 131rpx;
			border-radius: 10rpx;
		}

		.goods-intro {
			flex: 1;
			font-size: $font-base;
			line-height: 48rpx;
			margin-left: 30rpx;

			view:nth-child(1) {
				line-height: 1.4em;
				font-size: 24rpx;
				max-height: 2.8em; //height是line-height的整数倍，防止文字显示不全
				overflow: hidden;
				color: #666;
			}

			view:nth-child(2) {
				color: #cccccc;
				font-size: 24rpx;
			}

			view:nth-child(3) {
				color: $light-color;
			}
		}

		button {
			color: $main-color;
			height: 50rpx;
			width: 120rpx;
			font-size: $font-sm;
			padding: 0;
			line-height: 50rpx;
			background-color: #ffffff;
			margin-top: 80rpx;

			&::after {
				border-color: $main-color;
			}
		}
	}

	.store {
		background-color: #fff;
		border: 1px solid $border-color-light;
		border-radius: 16rpx;
		margin: 20rpx 10rpx;

		.intro {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 30rpx 0 40rpx;
			height: 170rpx;

			.store-logo {
				width: 102rpx;
				height: 102rpx;
				border-radius: 50%;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}

			.store-name {
				flex: 1;
				margin-left: 30rpx;
				line-height: 2em;

				:first-child {
					font-size: $font-base;
				}

				:last-child {
					font-size: $font-sm;
					color: #999;
				}
			}

			.store-collect {
				border-left: 1px solid $border-color-light;
				padding-left: 20rpx;
				text-align: center;

				:last-child {
					color: #999;
					font-size: $font-sm;
				}
			}
		}
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 26rpx;

			text {
				position: relative;
			}

			text.current {
				color: $light-color;
				font-weight: bold;
				font-size: 28rpx;

				&::after {
					content: "";
					position: absolute;
					left: 20rpx;
					bottom: -10rpx;
					width: 30rpx;
					height: 0;
					border-bottom: 2px solid $light-color;
				}
			}
		}
	}
	.u-tabs {
		width: 100%;
	}
	.u-tab-item {
		width: 50% !important;
	}
</style>